/**
 * @author tangjia
 * @desc 获取列配置对应prop的searchConfig的name属性
 * @param list：列配置数组
 * @param prop：对应的prop
 * @returns {*}
 */
function getColDictId(list, prop) {
  let searchConfig = list.find(item => item.prop === prop).searchConfig || {}
  return searchConfig.name
}

/**
 * @author liubaohui
 * @desc 获取表格列筛选需要的合并属性
 * @example
 * handleFilterChange(filters) {
 *  ...
 *  let filterParams = this.$filterParams(this.tableColumns, filters)
 *  Object.assign(this.searchParams, filterParams.search)
 *  Object.assign(this.sortOrder, filterParams.order)
 *  ...
 * }
 * @param tableColumns 列配置
 * @param filters @filter-change的回调值
 * @returns {{search: {}, order: {}}}
 */
function filterParams(tableColumns = [], filters = {}) {
  let keyLabel = Object.keys(filters)[0]
  let keyId = getColDictId(tableColumns, keyLabel)
  let valueId = Object.values(filters)[0].toString()
  return {
    search: {[keyId]: valueId === '' ? void 0 : valueId},
    order: {[keyLabel]: valueId === '' ? void 0 : 'desc'},
  }
}

export {getColDictId, filterParams}
